<?php
/**
 * 读取该条community数据并关联的子级zones
 */
$community['id'] = $data['community_id'];
$community = D('Community')->relation('zones')->find($community['id']);
/**
 * 判断该条数据中的zone是否已经存在在zone数据表中
 * 如果不存在就插入到zone数据表中，并父级community表中对应的数据children_count字段增1
 * 读取该条zone数据并关联的子级buildings
 */
if(is_int(array_search($data['zone_name'],array_column($community['zones'],'name')))){
    $zone = M('Zone')->where(array('community_id'=>$community['id'],'name'=>$data['zone_name']))->relation('buildings')->find();
}else{
    $zone['no'] = $community['no'].sprintf('%02d',($community['children_count']+1));
    $zone['name'] = $data['zone_name'];
    $zone['ename'] = '';
    $zone['status'] = 1;
    $zone['remark'] = '';
    $zone['community_id'] = $community['id'];
    $zone['full_parent_name'] = $community['full_parent_name'].$community['name'];
    $zone['children_count'] = 0;

    $zone['id'] = M('Zone')->add($zone);
    $zone = D('Zone')->relation('buildings')->find($zone['id']);

    M('Community')->where(array('id'=>$community['id']))->setInc('children_count',1);
}
/**
 * 判断该条数据中的building是否已经存在在building数据表中
 * 如果不存在就插入到building数据表中，并父级zone表中对应的数据children_count字段增1
 * 读取该条building数据并关联的子级units
 */
if(is_int(array_search($data['building_name'],array_column($zone['buildings'],'name')))){
    $building['id'] = M('Building')->where(array('zone_id'=>$zone['id'],'name'=>$data['building_name']))->relation('units')->find();
}else{
    $building['no'] = $zone['no'].sprintf('%03d',($zone['children_count']+1));
    $building['name'] = $data['building_name'];
    $building['ename'] = '';
    $building['status'] = 1;
    $building['remark'] = '';
    $building['community_id'] = $community['id'];
    $building['zone_id'] = $zone['id'];
    $building['full_parent_name'] = $zone['full_parent_name'].$zone['name'];
    $building['children_count'] = 0;

    $building['id'] = M('Building')->add($building);
    $building = D('Building')->relation('units')->find($building['id']);

    M('Zone')->where(array('id'=>$zone['id']))->setInc('children_count',1);
}
/**
 * 判断该条数据中的unit是否已经存在在unit数据表中
 * 如果不存在就插入到unit数据表中，并父级building表中对应的数据children_count字段增1
 * 读取该条unit数据并关联的子级rooms
 */
if(is_int(array_search($data['unit_name'],array_column($building['units'],'name')))){
    $unit['id'] = M('Unit')->where(array('building_id'=>$building['id'],'name'=>$data['unit_name']))->relation('rooms')->find();
}else{
    $unit['no'] = $building['no'].sprintf('%01d',($building['children_count']+1));
    $unit['name'] = $data['unit_name'];
    $unit['ename'] = '';
    $unit['status'] = 1;
    $unit['remark'] = '';
    $unit['community_id'] = $community['id'];
    $unit['zone_id'] = $zone['id'];
    $unit['building_id'] = $building['id'];
    $unit['full_parent_name'] = $building['full_parent_name'].$building['name'];
    $unit['children_count'] = 0;

    $unit['id'] = M('Unit')->add($unit);
    $unit = D('Unit')->relation('rooms')->find($unit['id']);

    M('Building')->where(array('id'=>$building['id']))->setInc('children_count',1);
}
/**
 * 判断该条数据中的room是否已经存在在room数据表中
 * 如果不存在就插入到room数据表中，并unit表中对应的数据children_count字段增1
 * 读取该条room数据并关联的子级families
 */
if(is_int(array_search($data['room_name'],array_column($building['rooms'],'name')))){
    $zone['id'] = M('Room')->where(array('unit_id'=>$unit['id'],'name'=>$data['unit_name']))->find();
}else{
    $room['no'] = $unit['no'].sprintf('%04d',($unit['children_count']+1));
    $room['name'] = $data['room_name'];
    $room['ename'] = '';
    $room['status'] = 1;
    $room['remark'] = '';
    $room['community_id'] = $community['id'];
    $room['zone_id'] = $zone['id'];
    $room['building_id'] = $building['id'];
    $room['unit_id'] = $unit['id'];
    $room['full_parent_name'] = $unit['full_parent_name'].$unit['name'];
    $room['children_count'] = 0;
    $room['type'] = $data['type'];
    $room['area'] = $data['area'];
    $room['toward'] = $data['toward'];
    $room['owner_id_no'] = $data['owner_id_no'];
    $room['owner_name'] = $data['owner_name'];
    $room['owner_mobi'] = $data['owner_mobi'];
    $room['owner_phone'] = $data['owner_phone'];
    $room['owner_contact'] = $data['owner_contact'];
    $room['householder_id_no'] = $data['householder_id_no'];
    $room['householder_name'] = $data['householder_name'];
    $room['householder_mobi'] = $data['householder_mobi'];
    $room['householder_phone'] = $data['householder_phone'];
    $room['householder_contact'] = $data['householder_contact'];
    $room['other_household'] = $data['other_household'];
    $room['householder_old_address'] = $data['householder_old_address'];
    $room['car'] = $data['car'];
    $room['parking'] = $data['parking'];

    $room['id'] = M('Room')->add($room);
    $room = D('Room')->relation('families')->find($room['id']);

    M('Unit')->where(array('id'=>$unit['id']))->setInc('children_count',1);
}

$count = M('room')->max('id');
for($i=1;$i<=$count/100+1;$i++){
    $rooms = M('room')->page($i,100)->select();
    foreach($rooms as $k=>$v){
        if(M('Customer')->where(array('mobi'=>$v['mobi']))->find()){

        }else{
            $customer['customer_no'] = date('YmdHis',time()).rand(1000,9999);
            $customer['id_no'] = $v['householder_id_no'] ? $v['householder_id_no'] : null;
            $customer['name'] = $v['householder_name'];
            $customer['mobi'] = $v['householder_mobi'];
            $customer['phone'] = $v['householder_phone'];
            $customer['contact'] = $v['householder_contact'];
            $customer['sex'] = 'M';
            $customer['birthday'] = $v['householder_id_no'] ? getBirthday($v['householder_id_no']) : null;
            $customer['ugroup'] = 1;
            $customer['status'] = 1;
            $customer['create_time'] = time();
            $customer['modify_time'] = time();
            $customer['creator_id'] = 5;
            $customer['modifier_id'] = 5;

            $customer_id = M('Customer')->add($customer);

            $family['no'] = $v['no'].sprintf('%02d',($v['children_count']+1));
            $family['community_id'] = $v['community_id'];
            $family['zone_id'] = $v['zone_id'];
            $family['building_id'] = $v['building_id'];
            $family['unit_id'] = $v['unit_id'];
            $family['room_id'] = $v['id'];
            $family['full_parent_name'] = $v['full_parent_name'];
            $family['householder'] = $customer_id;
            $family['checkin_date'] = null;
            $family['status'] = 1;
            $family['create_time'] = time();
            $family['modify_time'] = time();
            $family['delete_time'] = null;

            $family_id = M('Family')->add($family);

            $family_member['family_id'] = $family_id;
            $family_member['customer_id'] = $customer_id;
            $family_member['prefix'] = '户主';
            $family_member['title'] = null;
            $family_member['name'] = $customer['name'];
            $family_member['create_time'] = time();
            $family_member['modify_time'] = time();

            M('FamilyMember')->add($family_member);
        }
    }
}